package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;

import networkmapping.NetworkMappingDialog;

import jpcap.NetworkInterface;
import options.view.OptionsWindow;
import view.FrameWork;
import view.Informations;
import capture.Captor;

public class FrameWorkListener {
	private FrameWork frameWork = null;
	
	public FrameWorkListener(FrameWork frameWork) {
		this.frameWork = frameWork;
		
		this.frameWork.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent windEvn){
				FrameWorkListener.this.onClose();
			}; 
			
			public void windowDeiconified(WindowEvent windEvn){
				FrameWorkListener.this.onResize();				
			};
		});
		
		this.frameWork.addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				FrameWorkListener.this.onResize();
			};
		});
		
		this.frameWork.getExitMenuItem().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FrameWorkListener.this.onClose();
			}
		});
		
		this.frameWork.getAboutMenuItem().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Informations(FrameWorkListener.this.frameWork);
			}
		});
		
		this.frameWork.getOptionsMenuItem().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new OptionsWindow(FrameWorkListener.this.frameWork);
			}
		});
		
//		this.frameWork.getButtonSettings().addActionListener(this.frameWork.getOptionsMenuItem().getActionListeners()[0]);
		
		this.frameWork.getButtonShutDown().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FrameWorkListener.this.onClose();
			}
		});
		
		this.frameWork.getButtonStartCapture().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FrameWorkListener.this.checkAvailability();
				FrameWorkListener.this.frameWork.getCaptor().capturePacketsFromDevice();
				
			}
		});
		
		this.frameWork.getButtonStopCapture().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FrameWorkListener.this.checkAvailability();
				FrameWorkListener.this.frameWork.getCaptor().stopCapture();
				FrameWorkListener.this.frameWork.enableCapture();
			}
		});
		
		this.frameWork.getButtonStatistics().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		
		this.frameWork.getButtonMapping().addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				new NetworkMappingDialog(FrameWorkListener.this.frameWork).setVisible(true);
			}
			
		});
	}

	public void onResize(){
		try{
			this.frameWork.getMainFrame().setFullSize();
		} catch(NullPointerException nullExc) {
		}
	}
	
	private void onClose(){
		saveProperty();
		System.exit(0);
	}
	
	public void checkAvailability() {
		try{
			Class.forName("jpcap.JpcapCaptor");
			NetworkInterface[] devices=jpcap.JpcapCaptor.getDeviceList();
			if(devices.length==0){
				JOptionPane.showMessageDialog(null,"Nessuna interfaccia di rete trovata.\n Bisogna essere amministratori per poter effettuare la cattura dei pacchetti.",
						"Warning",JOptionPane.WARNING_MESSAGE);
			}
		}catch(ClassNotFoundException es){
			JOptionPane.showMessageDialog(null,"Bisogna installare Jpcap per eseguire questa funzionalita'.",
					"Error",JOptionPane.ERROR_MESSAGE);
			System.exit(-1);
		}catch(UnsatisfiedLinkError es){
			JOptionPane.showMessageDialog(null,"Bisogna installare Jpcap e/o libpcap/WinPcap per eseguire questa funzionalita'",
					"Error",JOptionPane.ERROR_MESSAGE);
			System.exit(-1);
		}
	}
	
	public void saveProperty(){
		Captor.preferences.put("WinWidth", String.valueOf(frameWork.getWidth()));
		Captor.preferences.put("WinHeight", String.valueOf(frameWork.getHeight()));
		Captor.preferences.put("WinX", String.valueOf(frameWork.getX()));
		Captor.preferences.put("WinY", String.valueOf(frameWork.getY()));

		frameWork.getCapturePanel().saveProperty();

		Captor.saveProperty();
	}
	

}
